package com.liuyucheng.java8.localdate;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * LocalDate是jdk1.8的新特性之一，提供了很多关于日期的API，对日期的操作变得更加方便
 */
public class TestLocalDate {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.minusDays(7);
        System.out.println("localDateTime = " + localDateTime);

        //LocalDateTime转换为Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("date = " + date);


//        LocalDate today = LocalDate.now();
//        //这个月是几号
//        int dayOfMonth = today.getDayOfMonth();
//        System.out.println("这个月是几月:"+today.getMonthValue());
//        System.out.println("今天是周几:"+today.getDayOfWeek());
//        System.out.println(dayOfMonth);
//
//        LocalDateTime time = LocalDateTime.now();
//        System.out.println(time.getYear());
//        System.out.println(time.getMonthValue());
//        System.out.println(time.getDayOfMonth());
//        System.out.println(time.getHour());
//        System.out.println(time.getMinute());
//        System.out.println(time.getSecond());
//        System.out.println(time.getNano());
//
//        // 取本月第1天：
//        LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2018-04-01
//        // 取本月第2天：
//        LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2018-04-02
//        // 取本月最后一天，再也不用计算是28，29，30还是31：
//        LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2018-04-30
//        // 取下一天：
//        LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusDays(1); // 变成了2018-05-01
//        // 取2017年1月第一个周一：
//        LocalDate firstMondayOf2017 = LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2017-01-02
//
//
//        System.out.println(firstDayOfThisMonth);
//        System.out.println(secondDayOfThisMonth);
//        System.out.println(lastDayOfThisMonth);
//        System.out.println(firstDayOfNextMonth);
//        System.out.println(firstMondayOf2017);
    }
}
